home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / bin / passmass < prev    next >
Text File  |  1995-07-21  |  4KB  |  190 lines

  1. #!/usr/skunk/bin/expect --
  2. # passmass: change password on many machines
  3. # Synopsis: passmass host1 host2 host3 ....
  4. # Don Libes - March 11, 1991
  5.  
  6. # Description: Change passwords on the named machines.
  7. #
  8. # You are prompted for old/new passwords.  (If you are changing root
  9. # passwords and have equivalencing, the old password is not used and may be
  10. # omitted.)
  11. # Additional arguments may be used for fine tuning.  They affect all hosts
  12. # which follow until another argument overrides.
  13. #
  14. #   -user    User whose password will be changed.  By default, the current
  15. #        user is used.
  16. #   -rlogin    Use rlogin to access host.  (default)
  17. #   -telnet    Use telnet to access host.
  18. #   -program    Next argument is taken as program to run to set password.
  19. #        Default is "passwd".  Other common choices are "yppasswd" and
  20. #        "set passwd" (e.g., VMS hosts).
  21. #   -prompt    Next argument is taken as a prompt suffix pattern.  This allows
  22. #        the script to know when the shell is prompting.  The default is
  23. #        "# " for root and "% " for non-root accounts.
  24. #   -timeout    Next argument is number of seconds to wait for responses.
  25. #        Default is 30 but some systems can be much slower logging in.
  26.  
  27. # The best way to run this is to put the command in a one-line shell script
  28. # or alias. (Presumably, the set of hosts and parameters will rarely change.)
  29. # Then run it whenever you want to change your passwords on all the hosts.
  30.  
  31. exp_version -exit 5.0
  32.  
  33. if $argc==0 {
  34.     send_user "usage: $argv0 host1 host2 host3 . . .\n"
  35.     exit
  36. }
  37.  
  38. expect_before -i $user_spawn_id \003 exit
  39.  
  40. proc badhost {host emsg} {
  41.     global badhosts
  42.  
  43.     send_user "\r\n\007password not changed on $host - $emsg\n\n"
  44.     if 0==[llength $badhosts] {
  45.         set badhosts $host
  46.     } else {
  47.         set badhosts [concat $badhosts $host]
  48.     }
  49. }
  50.  
  51. # set defaults
  52. set login "rlogin"
  53. set program "passwd"
  54. set user [exec whoami]
  55.  
  56. set timeout 1000000
  57. stty -echo
  58. send_user "old password: "
  59. expect_user -re "(.*)\n"
  60. send_user "\n"
  61. set oldpassword $expect_out(1,string)
  62. send_user "new password: "
  63. expect_user -re "(.*)\n"
  64. send_user "\n"
  65. set newpassword $expect_out(1,string)
  66. send_user "retype new password: "
  67. expect_user -re "(.*)\n"
  68. set newpassword2 $expect_out(1,string)
  69. send_user "\n"
  70. stty echo
  71. trap exit SIGINT
  72.  
  73. if ![string match $newpassword $newpassword2] {
  74.     send_user "mismatch - password unchanged\n"
  75.     exit
  76. }
  77.  
  78.  
  79. #send_user "want to see new password you just typed? (y|n) "
  80. #expect_user "*\n"
  81. #
  82. #if [string match "y" [lindex $expect_match 0 c]] {
  83. #    send_user "password is <$newpassword>\nproceed? (y|n) "
  84. #    expect_user "*\n"
  85. #    if ![string match "y" [lindex $expect_match 0 c]] exit
  86. #}
  87.  
  88. set timeout 30
  89. set badhosts {}
  90. for {set i 0} {$i<$argc} {incr i} {
  91.  
  92.     set arg [lindex $argv $i]
  93.     switch -- $arg \
  94.         "-user" {
  95.             incr i
  96.             set user [lindex $argv $i]
  97.             continue
  98.         } "-prompt" {
  99.             incr i
  100.             set prompt [lindex $argv $i]
  101.             continue
  102.         } "-rlogin" {
  103.             set login "rlogin"
  104.             continue
  105.         } "-telnet" {
  106.             set login "telnet"
  107.             continue
  108.         } "-program" {
  109.             incr i
  110.             set program [lindex $argv $i]
  111.             continue
  112.         } "-timeout" {
  113.             incr i
  114.             set timeout [lindex $argv $i]
  115.             continue
  116.         }
  117.  
  118.     set host $arg
  119.     if [string match $login "rlogin"] {
  120.         set pid [spawn rlogin $host -l $user]
  121.     } else {
  122.         set pid [spawn telnet $host]
  123.         expect -re "(login|Username):.*" {
  124.             send "$user\r"
  125.         }
  126.     }
  127.  
  128.     if ![info exists prompt] {
  129.         if [string match $user "root"] {
  130.             set prompt "# "
  131.         } else {
  132.             set prompt "(%|\\\$) "
  133.         }
  134.     }
  135.  
  136.     set logged_in 0
  137.     for {} 1 {} {
  138.         expect "Password*" {
  139.             send "$oldpassword\r"
  140.         } eof {
  141.             badhost $host "spawn failed"
  142.             break
  143.         } timeout {
  144.             badhost $host "could not log in (or unrecognized prompt)"
  145.             exec kill $pid
  146.             expect eof
  147.             break
  148.         } -re "incorrect|invalid" {
  149.             badhost $host "bad password or login"
  150.             exec kill $pid
  151.             expect eof
  152.             break
  153.         } -re $prompt {
  154.             set logged_in 1
  155.             break
  156.         }
  157.     }
  158.  
  159.     if (!$logged_in) {
  160.         wait
  161.         continue
  162.     }
  163.  
  164.     send "$program\r"
  165.     expect "Old password*" {
  166.         send "$oldpassword\r"
  167.         expect "Sorry*" {
  168.             badhost $host "old password is bad?"
  169.             continue
  170.         } "password:"
  171.     } -re "(n|N)ew password:"
  172.     send "$newpassword\r"
  173.     expect -re "not changed|unchanged" {
  174.         badhost $host "new password is bad?"
  175.         continue
  176.     } -re "(password|Verification|Verify|again):.*"
  177.     send "$newpassword\r"
  178.     expect -re "(not changed|incorrect|choose new).*" {
  179.         badhost $host "password is bad?"
  180.         continue
  181.     } -re "$prompt"
  182.     send_user "\n"
  183.  
  184.     close
  185.     wait
  186. }
  187.  
  188. if [llength $badhosts] {send_user "\nfailed to set password on $badhosts\n"}
  189.